<?php

class Member extends CI_Controller {

	function __construct()
	{
		parent:: __construct();
		$this->load->helper('bantuan_helper');
		$this->load->model('Member_model');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		elseif($this->session->userdata('user_rule')=='Sales'):
			redirect('backend/no_acces');
		endif;

	}

	function index()
	{
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/member/index';
		$config['total_rows'] = count($this->Member_model->getMember('all',FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
			echo $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Member';
		$data['template'] = 'member/index';
		$data['res'] = $this->Member_model->getMember('all',FALSE,$config['per_page'],$offset);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}
	
	function add()
	{
		$this->form_validation->set_rules('nm_member', 'Nama Member','required');
		$this->form_validation->set_rules('date_of_birth', 'Tanggal Lahir','required');
		$this->form_validation->set_rules('sex', 'Jenis Kelamin Member','required');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('address', 'Alamat Member','required');
		$this->form_validation->set_rules('no_hp', 'No.HP Member','required');
		$this->form_validation->set_rules('id_province', 'Provinsi Member','required');
		$this->form_validation->set_rules('id_kab', 'Kabupaten Member','required');
		$this->form_validation->set_rules('post_code', 'Kode Pos Member','required|exact_length[5]|numeric');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[retype_pass]');
		$this->form_validation->set_rules('retype_pass', 'Password Confirmation', 'trim|required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['title'] = 'Member';
			$data['template'] = 'member/add';
			$this->load->view('backend/index',$data);
		}
		else
		{
			$data = array
				(
				'nm_member'=>$this->input->post('nm_member'),
				'date_of_birth'=>$this->input->post('date_of_birth'),
				'sex'=>$this->input->post('sex'),
				'email'=>$this->input->post('email'),
				'address'=>$this->input->post('address'),
				'no_hp'=>$this->input->post('no_hp'),
				'id_province'=>$this->input->post('id_province'),
				'id_kab'=>$this->input->post('id_kab'),
				'post_code'=>$this->input->post('retype_pass'),
				'password'=>md5($this->input->post('password'))
				);
			$this->Member_model->addMember($data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Satu data Berhasil ditambahkan</div>');
			redirect('backend/member');
		}	
	}

	function edit()
	{
		$this->form_validation->set_rules('nm_member', 'Nama Member','required');
		$this->form_validation->set_rules('date_of_birth', 'Tanggal Lahir','required');
		$this->form_validation->set_rules('sex', 'Jenis Kelamin Member','required');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('address', 'Alamat Member','required');
		$this->form_validation->set_rules('no_hp', 'No.HP Member','required');
		$this->form_validation->set_rules('id_province', 'Provinsi Member','required');
		$this->form_validation->set_rules('id_kab', 'Kabupaten Member','required');
		$this->form_validation->set_rules('post_code', 'Kode Pos Member','required|exact_length[5]|numeric');		
		
		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$data['title'] = 'Member';
			$data['template'] = 'member/edit';
								
			$data['row'] = $this->Member_model->getMember(FALSE,$id,FALSE,FALSE);
			$this->load->view('backend/index',$data);
		}
		else
		{	
			$id = $this->uri->segment(4);
			if($this->input->post('password') != ''){
			$data = array
					(
					'nm_member'=>$this->input->post('nm_member'),
					'date_of_birth'=>$this->input->post('date_of_birth'),
					'sex'=>$this->input->post('sex'),
					'email'=>$this->input->post('email'),
					'address'=>$this->input->post('address'),
					'no_hp'=>$this->input->post('no_hp'),
					'id_province'=>$this->input->post('id_province'),
					'id_kab'=>$this->input->post('id_kab'),
					'post_code'=>$this->input->post('post_code'),
					'password'=>md5($this->input->post('password'))
					);			
			}
			else{
				$data = array
					(
					'nm_member'=>$this->input->post('nm_member'),
					'date_of_birth'=>$this->input->post('date_of_birth'),
					'sex'=>$this->input->post('sex'),
					'email'=>$this->input->post('email'),
					'address'=>$this->input->post('address'),
					'no_hp'=>$this->input->post('no_hp'),
					'id_province'=>$this->input->post('id_province'),
					'id_kab'=>$this->input->post('id_kab'),
					'post_code'=>$this->input->post('post_code')
					);
			}
			$this->Member_model->editMember($id,$data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil diupdate</div>');
			redirect('backend/member/edit/'.$id);
		}
			
	}

	function delete($id)
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$this->Member_model->deleteMember($id);
		$count = count($this->Member_model->getMember('all',FALSE,FALSE,FALSE));
		$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil dihapus</div>');
		getPage('member','delete',$uri,$count);
	}
	
	function ajax_bulk_action()
	{
		foreach($_POST["member_id"] as $id):
			$this->db->where('id_member',$id);
			$this->db->delete('tk_member');
		endforeach;
	}
	
	function ajax_showKab(){
		$provId=$this->uri->segment(4);
		$this->db->where('id_province',$provId);
		$data['kab']= $this->db->get('tk_kab')->result_array();
		$this->load->view('backend/marketing/help_kab',$data);
	}

}